package com.yanqu.road.server.channelserver.timer;

import com.yanqu.road.server.TempServer;
import com.yanqu.road.server.channelserver.timer.activity.ChannelXUnionTask;
import com.yanqu.road.server.channelserver.timer.entername.ChannelEnterNameTask;
import com.yanqu.road.server.channelserver.timer.scene.SyncPlayerSceneActionTask;
import com.yanqu.road.server.channelserver.timer.server.ChannelXServerNameTask;
import com.yanqu.road.server.channelserver.timer.tonbing.ChannelTonBingTimerTask;
import com.yanqu.road.server.channelserver.timer.washe.ChannelWasheTask;
import com.yanqu.road.server.channelserver.timer.xrank.ChannelXRankTask;
import com.yanqu.road.server.timer.activity.CrossXUserBaseInfoClearTask;
import com.yanqu.road.server.timer.log.ChannelAutoLogSaveTask;
import com.yanqu.road.utils.ServerType;
import com.yanqu.road.utils.executor.GeneralTask;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 所有的定时器写在这边
 */
public class ChannelServerTimerMgr {

    private static final Logger logger = LogManager.getLogger(ChannelServerTimerMgr.class.getName());
    private static ScheduledExecutorService timerExecutorService = null;

    private static ChannelConnectionCheckTask connectionCheckTask = null;
    private static ChannelMgrDataSaveTask mgrDataSaveTask = null;

    private static ChannelXRankTask channelXRankTask = null;

    private static ChannelXServerNameTask channelXServerNameTask = null;

    private static CrossXUserBaseInfoClearTask crossXUserBaseInfoClearTask = null;

    private static ChannelWasheTask channelWasheTask = null;

    private static ChannelLogSaveTask channelLogSaveTask = null;

    private static ChannelEnterNameTask channelEnterNameTask = null;

    private static ChannelXUnionTask channelXUnionTask = null;

    private static ChannelTonBingTimerTask channelTonBingTimerTask;

    private static SyncPlayerSceneActionTask syncPlayerSceneActionTask;

    private static ChannelWashPkTask channelWashPkTask = null;

    private static ChannelAutoLogSaveTask channelAutoLogSaveTask = null;

    public static synchronized boolean initGeneralTimers(int serverType) {
        timerExecutorService = TempServer.getTimerExecutorService();

        if (null == connectionCheckTask) {
            connectionCheckTask = new ChannelConnectionCheckTask(ServerType.SERVER_CHANNEL);
            timerExecutorService.scheduleWithFixedDelay(connectionCheckTask, 0, 5, TimeUnit.SECONDS);
        }

        if (null == mgrDataSaveTask) {
            mgrDataSaveTask = new ChannelMgrDataSaveTask();
            timerExecutorService.scheduleWithFixedDelay(mgrDataSaveTask, 0, 60, TimeUnit.SECONDS);
        }

        if (null == channelXRankTask) {
            channelXRankTask = new ChannelXRankTask();
            timerExecutorService.scheduleWithFixedDelay(channelXRankTask, 10, 2, TimeUnit.SECONDS);
        }

        if (null == channelXServerNameTask) {
            channelXServerNameTask = new ChannelXServerNameTask();
            timerExecutorService.scheduleWithFixedDelay(channelXServerNameTask, 10, 30, TimeUnit.MINUTES);
        }

        if (null == crossXUserBaseInfoClearTask) {
            crossXUserBaseInfoClearTask = new CrossXUserBaseInfoClearTask();
            timerExecutorService.scheduleWithFixedDelay(crossXUserBaseInfoClearTask, 5, 30, TimeUnit.MINUTES);
        }

        if (null == channelWasheTask) {
            channelWasheTask = new ChannelWasheTask();
            timerExecutorService.scheduleWithFixedDelay(channelWasheTask, 15, 1, TimeUnit.SECONDS);
        }

        if (null == channelLogSaveTask) {
            channelLogSaveTask = new ChannelLogSaveTask();
            timerExecutorService.scheduleWithFixedDelay(channelLogSaveTask, 30, 10, TimeUnit.SECONDS);
        }

        if (null == channelEnterNameTask) {
            channelEnterNameTask = new ChannelEnterNameTask();
            timerExecutorService.scheduleWithFixedDelay(channelEnterNameTask, 100, 30, TimeUnit.SECONDS);
        }

        if (null == channelXUnionTask) {
            channelXUnionTask = new ChannelXUnionTask();
            timerExecutorService.scheduleWithFixedDelay(channelXUnionTask, 10, 20, TimeUnit.SECONDS);
        }

        if (channelTonBingTimerTask == null) {
            channelTonBingTimerTask = new ChannelTonBingTimerTask();
            timerExecutorService.scheduleWithFixedDelay(channelTonBingTimerTask, 10, 10, TimeUnit.SECONDS);
        }

        if (null == syncPlayerSceneActionTask) {
            syncPlayerSceneActionTask = new SyncPlayerSceneActionTask();
            timerExecutorService.scheduleWithFixedDelay(syncPlayerSceneActionTask, 1, 1, TimeUnit.SECONDS);
        }

        if (null == channelWashPkTask) {
            channelWashPkTask = new ChannelWashPkTask();
            timerExecutorService.scheduleWithFixedDelay(channelWashPkTask, 15, 10, TimeUnit.SECONDS);
        }

        if (null == channelAutoLogSaveTask) {
            channelAutoLogSaveTask = new ChannelAutoLogSaveTask();
            timerExecutorService.scheduleWithFixedDelay(channelAutoLogSaveTask, 60, 30, TimeUnit.SECONDS);
        }

        return true;
    }

    public static void newStop() {
        cancelTask(connectionCheckTask);
        cancelTask(mgrDataSaveTask);
        cancelTask(channelXRankTask);
        cancelTask(channelXServerNameTask);
        cancelTask(crossXUserBaseInfoClearTask);
        cancelTask(channelWasheTask);
        cancelTask(channelLogSaveTask);
        cancelTask(channelEnterNameTask);
        cancelTask(channelXUnionTask);
        cancelTask(channelTonBingTimerTask);
    }

    private static void cancelTask(GeneralTask task) {
        if (null != task) {
            task.cancel(false);
        }
    }

}
